<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>

      <el-input v-model="params.carNumber" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">车主姓名：</span>

      <el-input v-model="params.personName" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">状态：</span>

      <el-select v-model="params.cardStatus">
        <el-option
          v-for="item in selectStatus"
          :key="item.id"
          :value="item.id"
          :label="item.value"
        />
      </el-select>

      <el-button type="primary" class="search-btn" @click="selectFn">查询</el-button>

    </div>

    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/cardAdd')">添加月卡</el-button>

      <el-button @click="delALL">批量删除</el-button>

    </div>

    <!-- 表格区域 -->
    <div class="table">
      <el-table
        style="width: 100%"
        :data="cardlist"
        @current-change="handleCurrentChange"
      >
        <el-table-column
          type="selection"
          width="55"
        />
        <el-table-column type="index" label="序号" />
        <!-- prop="给表格关联字段" -->
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column label="状态" prop="cardStatus" :formatter="formatter" />
        <el-table-column label="操作" fixed="right" width="180">
          <!-- #default="scope" -->
          <template #default="{row}">
            <el-button size="mini" type="text" @click="renewFn(row)">续费</el-button>

            <el-button size="mini" type="text" @click="lookFn(row)">查看</el-button>

            <el-button size="mini" type="text" @click="editFn(row)">编辑</el-button>

            <el-button size="mini" type="text" @click="delFn(row)">删除</el-button>

          </template>

        </el-table-column>

      </el-table>

    </div>

    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :page-size="params.pageSize"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 添加楼宇 -->
    <el-dialog
      title="添加楼宇"
      width="580px"
    >
      <template #footer>
        <el-button size="mini">取 消</el-button>

        <el-button size="mini" type="primary">确 定</el-button>

      </template>

    </el-dialog>

  </div>

</template>

<script>
import { delCardAPI, getCardListAPI } from '@/api/card'
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10,
        carNumber: '',
        personName: '',
        cardStatus: ''
      },
      cardlist: [],
      total: 0,
      selectStatus: [
        { id: 0, value: '可用' },
        { id: 1, value: '已过期' }
      ]
    }
  },
  async created() {
    this.getTablist()
  },
  methods: {
    // 月卡列表
    async getTablist() {
      const res = await getCardListAPI(this.params)
      this.cardlist = res.data.rows
      this.total = res.data.total
    },
    // 格式化状态
    formatter(row, column, cellvalue, index) {
      // cellvalue可以拿到当前单元格的数据
      const statusMap = {
        0: '可用',
        1: '已过期'
      }
      return statusMap[cellvalue]
    },
    // 分页器页码
    handleCurrentChange(val) {
      this.params.page = val
      this.getTablist()
    },
    // 查询
    selectFn() {
      this.params.page = 1
      this.getTablist()
    },
    //
    editFn(row) {
      this.$router.push({
        path: '/cardAdd',
        query: {
          id: row.id
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 单个删除
    delFn(row) {
      this.$confirm('确认删除吗？', '提示', {
        confirmButtonText: '确定',
        concelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delCardAPI([row.id])
        if (res.code !== 10000) {
          this.$message.error('删除失败，' + res.msg)
        } else {
          this.$message.success('删除成功')
          this.selectFn()
        }
      }).catch(err => {
        console.log(err)
      })
    },
    // 多个删除
    delALL() {
      const ids = this.multipleSelection.map(item => item.id)
      // console.log(ids)
      this.$confirm('确认删除吗？', '提示', {
        confirmButtonText: '确定',
        concelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delCardAPI(ids)
        if (res.code !== 10000) {
          this.$message.error('删除失败，' + res.msg)
        } else {
          this.$message.success('删除成功')
          this.selectFn()
        }
      }).catch(err => {
        console.log(err)
      })
    },
    // 续费
    async renewFn(row) {
      this.$router.push({
        path: '/renewMonthCard',
        query: {
          id: row.id
        }
      })
    },
    // 查看
    async lookFn(row) {
      this.$router.push({
        path: '/viewMonthCard',
        query: {
          id: row.id
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn{
    margin-left:20px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
